package com.tian.web.admin.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.oscache.util.StringUtil;
import com.tian.core.action.JqGridBaseAction;
import com.tian.core.bean.Condition;
import com.tian.core.bean.ConditionOperate;
import com.tian.core.bean.ConditionType;
import com.tian.core.bean.Pages;
import com.tian.core.entity.Depart;
import com.tian.core.entity.Group;
import com.tian.core.entity.Master;
import com.tian.web.admin.service.DepartService;
@Namespace("/admin")
@Results({
	@Result(type = "dispatcher",name="depart",location="/pages/admin/depart_list.jsp"),
})
public class DepartAction extends JqGridBaseAction {

	private static final long serialVersionUID = 442332299788276915L;

	@Resource
	private DepartService departService;
	
	private List<Depart> gridModel;
	
	private Depart depart;
	
	@Override
	public String refreshGridModel() {
		this.record = departService.getTotalCount(Depart.class);
		Pages pages = new Pages();		
    	pages.setPage(page);
    	pages.setTotalNum(record);
    	pages.setPerPageNum(rows);
    	pages.executeCount();
    	total = pages.getAllPage();
    	
    	if(!StringUtil.isEmpty(this.sidx) && !StringUtil.isEmpty(this.sord)){
    		Condition condition1 = new Condition();
        	condition1.setName(this.sidx);
    		//condition1.setValue(Condition.DESC);
    		condition1.setValue(this.sord);
    		condition1.setType(ConditionType.ORDER);
    		conditions.add(condition1);
    	}
    	//只实现equal
    	if(this.search && !StringUtil.isEmpty(this.searchField)
    			&& !StringUtil.isEmpty(this.searchOper)&& !StringUtil.isEmpty(this.searchString)){
    		Condition condition2 = new Condition();
    		condition2.setName(this.searchField);
    		//condition1.setValue(Condition.DESC);
    		condition2.setValue(this.searchString);
    		condition2.setType(ConditionType.PROPERTY);
    		condition2.setOperate(ConditionOperate.EQUAL);
    		conditions.add(condition2);
    	}
		
    	pageList = departService.findByPages(conditions, pages);
		pages = pageList.getPages();
		
        this.rows = pages.getPerPageNum();
        this.record = pages.getTotalNum();
        this.page = pages.getPage();
        
        gridModel = pageList.getObjectList();
        //设为null，否则无法转换为json对象
/*		for(Group m :gridModel){
			m.setActiongroups(null);
			m.setMastergroups(null);
			m.setZtrees(null);
		}*/
		System.out.println("rows:"+rows+" page:"+ page + " total:" + total + " record:" + record);
		return SUCCESS;
	}
	
	@Override
	public String execute() throws Exception {
		return "depart";
	}
	
	public String list() throws Exception {
		
		return this.refreshGridModel();
	}
	public String delete() throws Exception {
		if(StringUtil.isEmpty(this.id)){
    		return ERROR;
    	}
		departService.delete(Integer.parseInt(id));
		return ajax("ajax_ok");
	}
	public String addOrUpdate() throws Exception {
    	if(id!=null){//编辑
    		depart = departService.get(Integer.parseInt(id));
    	}else{
    		depart = new Depart();
    	}
    	return "update";
    }
	 public String save() throws Exception {
	    	
	    	if(depart.getId()!=null){//编辑
	    		Depart d = departService.get(depart.getId());
	    		d.setDeptname(depart.getDeptname());
	    		departService.update(d);
	    		
	    	}else{//新增
	    		departService.save(depart);
	    	}
	    	return "depart";
	    }
	
	public List<Depart> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<Depart> gridModel) {
		this.gridModel = gridModel;
	}

	public Depart getDepart() {
		return depart;
	}

	public void setDepart(Depart depart) {
		this.depart = depart;
	}	

}
